from pydantic import BaseModel
from backend.app.app.db.session_private import session_pri
import backend.app.app.models.db_owi_private_models as md_pri
from backend.app.app.db.session_public import session_pub
import backend.app.app.models.db_owi_public_models as md_pub
from backend.app.app.models.dvpunithist import data_save


#    读 ID
def get_u_id():
    try:
        result = session_pri.query(md_pri.TbYearDvpUnitProfitLossAn.user_id).all()
        return result
    except:
        print('查找失败')
        session_pri.rollback()
        return False


#    读 pro_ID
def get_p_id():
    try:
        result = session_pri.query(md_pri.TbYearDvpUnitProfitLossAn.project_id).all()

        return result
    except:
        print('查找失败')
        session_pri.rollback()
        return False


#   根据user_id查询pro_id
def get_p_id_u(user_id):
    try:
        result = session_pri.query(md_pri.TbYearDvpUnitProfitLossAn.project_id).filter(
            md_pri.TbYearDvpUnitProfitLossAn.user_id == user_id).all()
        return result
    except:
        print('查找失败')
        session_pri.rollback()
        return False


#   读 页面数据
def query_data(user_Id, Pro_Id):
    try:
        project_inf = session_pri.query(
            md_pri.TbYearDvpUnitProfitLossAn.user_id,
            md_pri.TbYearDvpUnitProfitLossAn.project_id,
            md_pri.TbYearDvpUnitProfitLossAn.dvpUnit_Name,
            md_pri.TbYearDvpUnitProfitLossAn.stimType_Name,
            md_pri.TbYearDvpUnitProfitLossAn.year,
            md_pri.TbYearDvpUnitProfitLossAn.calcu_type,
            md_pri.TbYearDvpUnitProfitLossAn.analysis_data,
            md_pri.TbYearDvpUnitProfitLossAn.parm_set_data,
            md_pri.TbYearDvpUnitProfitLossAn.OilPriceCritical,
            md_pri.TbYearDvpUnitProfitLossAn.IncreOilCritical,
            md_pri.TbYearDvpUnitProfitLossAn.StimCostCritical,
            md_pri.TbYearDvpUnitProfitLossAn.Stim_WellNumber_Valid,
            md_pri.TbYearDvpUnitProfitLossAn.Stim_WellValidRatio,
            md_pri.TbYearDvpUnitProfitLossAn.Stim_ValidPeriod_Average,
            md_pri.TbYearDvpUnitProfitLossAn.Stim_IncreOil_Average,
            md_pri.TbYearDvpUnitProfitLossAn.StimOil_CommodityRatio,
            md_pri.TbYearDvpUnitProfitLossAn.Oil_Price,
            md_pri.TbYearDvpUnitProfitLossAn.OilSale_TotalTaxRatio,
            md_pri.TbYearDvpUnitProfitLossAn.Money_Discount,
            md_pri.TbYearDvpUnitProfitLossAn.Oil_OprCost,
            md_pri.TbYearDvpUnitProfitLossAn.StimCost_Average,
            md_pri.TbYearDvpUnitProfitLossAn.DvpUnit_OilStim_PlanProj_Name,
            md_pri.TbYearDvpUnitProfitLossAn.Forecast_Method
        ).filter(
            md_pri.TbYearDvpUnitProfitLossAn.user_id == user_Id,
            md_pri.TbYearDvpUnitProfitLossAn.project_id == Pro_Id
        ).all()

        return project_inf
    except Exception as e:
        print(e)
        print('查找失败')
        session_pri.rollback()
        return False


#   存
def save_project_db(s_data):
    try:
        session_pri.add(md_pri.TbYearDvpUnitProfitLossAn(
            user_id=s_data.User_ID,
            project_id=s_data.Proj_ID,
            dvpUnit_Name=s_data.dvpUnit_Name,
            stimType_Name=s_data.stimType_Name,
            year=s_data.year,
            calcu_type=s_data.calculation_type,
            analysis_data=s_data.analysis_data,
            parm_set_data=s_data.parameter_set,
            OilPriceCritical=s_data.OilPriceCritical,
            IncreOilCritical=s_data.IncreOilCritical,
            StimCostCritical=s_data.StimCostCritical,
            Stim_WellNumber_Valid=s_data.Stim_WellNumber_Valid,
            Stim_WellValidRatio=s_data.Stim_WellValidRatio,
            Stim_ValidPeriod_Average=s_data.Stim_ValidPeriod_Average,
            Stim_IncreOil_Average=s_data.Stim_IncreOil_Average,
            StimOil_CommodityRatio=s_data.StimOil_CommodityRatio,
            Oil_Price=s_data.Oil_Price,
            OilSale_TotalTaxRatio=s_data.OilSale_TotalTaxRatio,
            Money_Discount=s_data.Money_Discount,
            Oil_OprCost=s_data.Oil_OprCost,
            StimCost_Average=s_data.StimCost_Average,
            DvpUnit_OilStim_PlanProj_Name=s_data.DvpUnit_OilStim_PlanProj_Name,
            Forecast_Method=s_data.Forecast_Method
        ))
        session_pri.commit()
        print('储存成功')
        return True
    except:
        print('储存失败')
        session_pri.rollback()
        return False


#   更新数据
def updata_project_db(s_data):
    try:
        session_pri.query(md_pri.TbYearDvpUnitProfitLossAn).filter(
            md_pri.TbYearDvpUnitProfitLossAn.user_id == s_data.User_ID,
            md_pri.TbYearDvpUnitProfitLossAn.project_id == s_data.Proj_ID) \
            .update(
            {
                md_pri.TbYearDvpUnitProfitLossAn.dvpUnit_Name: s_data.dvpUnit_Name,
                md_pri.TbYearDvpUnitProfitLossAn.stimType_Name: s_data.stimType_Name,
                md_pri.TbYearDvpUnitProfitLossAn.year: s_data.year,
                md_pri.TbYearDvpUnitProfitLossAn.calcu_type: s_data.calculation_type,
                md_pri.TbYearDvpUnitProfitLossAn.analysis_data: s_data.analysis_data,
                md_pri.TbYearDvpUnitProfitLossAn.parm_set_data: s_data.parameter_set,
                md_pri.TbYearDvpUnitProfitLossAn.OilPriceCritical: s_data.OilPriceCritical,
                md_pri.TbYearDvpUnitProfitLossAn.IncreOilCritical: s_data.IncreOilCritical,
                md_pri.TbYearDvpUnitProfitLossAn.StimCostCritical: s_data.StimCostCritical,
                md_pri.TbYearDvpUnitProfitLossAn.Stim_WellNumber_Valid: s_data.Stim_WellNumber_Valid,
                md_pri.TbYearDvpUnitProfitLossAn.Stim_WellValidRatio: s_data.Stim_WellValidRatio,
                md_pri.TbYearDvpUnitProfitLossAn.Stim_ValidPeriod_Average: s_data.Stim_ValidPeriod_Average,
                md_pri.TbYearDvpUnitProfitLossAn.Stim_IncreOil_Average: s_data.Stim_IncreOil_Average,
                md_pri.TbYearDvpUnitProfitLossAn.StimOil_CommodityRatio: s_data.StimOil_CommodityRatio,
                md_pri.TbYearDvpUnitProfitLossAn.Oil_Price: s_data.Oil_Price,
                md_pri.TbYearDvpUnitProfitLossAn.OilSale_TotalTaxRatio: s_data.OilSale_TotalTaxRatio,
                md_pri.TbYearDvpUnitProfitLossAn.Money_Discount: s_data.Money_Discount,
                md_pri.TbYearDvpUnitProfitLossAn.Oil_OprCost: s_data.Oil_OprCost,
                md_pri.TbYearDvpUnitProfitLossAn.StimCost_Average: s_data.StimCost_Average,
                md_pri.TbYearDvpUnitProfitLossAn.DvpUnit_OilStim_PlanProj_Name: s_data.DvpUnit_OilStim_PlanProj_Name,
                md_pri.TbYearDvpUnitProfitLossAn.Forecast_Method: s_data.Forecast_Method
            })
        session_pri.commit()
        return True
    except:
        session_pri.rollback()
        print("更新数据失败")
        return False


# 获取单元措施方案 hzc增
def get_dvpunit_planproj():
    try:
        plan_data = session_pub.query(
            md_pub.TbYearDvpunitOilstimPlanprojForecast.DvpUnit_OilStim_PlanProj_Name
        ).distinct().all()
        if plan_data:
            plan_data = [data[0] for data in plan_data]
        else:
            return None
        # print(plan_data)
        return plan_data
    except Exception as e:
        print(e.__traceback__.tb_lineno)
        print(e)
        return False
